﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Models;
using BLL;

namespace CSMF.Management.Page_Video
{
    public partial class Video_Detail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["admin"] != null)
                {
                    BindDDLType();
                    int id = Convert.ToInt32(Request.QueryString["id"]);
                    Models.VideoInfo photo = VideoInfoManager.GetVideoInfoByID(id);
                    string[] cloths = photo.TypeID.Split(',');
                    List<string> list = new List<string>(cloths);
                    for (int i = 0; i < list.Count; i++)
                    {
                        for (int j = 0; j < cbVideoType.Items.Count; j++)
                        {
                            if (cbVideoType.Items[j].Value.Equals(list[i]))
                            {
                                cbVideoType.Items[j].Selected = true;
                            }
                        }
                    }
                    txtXUHAO.Text = photo.XUHAO.ToString();
                    txtTitle.Text = photo.Title;
                    txtPoint.Text = photo.Point.ToString();
                    txtCount.Text = photo.Count.ToString();
                    FCKeditor1.Value = photo.VideoContent;
                    lblILogo.Text = photo.Image;
                    imgInsertLogo.ImageUrl = "../../UpLoad/videoImage/" + photo.Image;
                    txtAuthor.Text = photo.Author;
                    Label1.Text = photo.AuthorPhoto;
                    Image1.ImageUrl = "../../UpLoad/videoImage/" + photo.AuthorPhoto;
                }
                else
                {
                    Response.Write("<script language='javascript'>parent.location.href='../login.aspx';</script>");
                }
            }
        }

        private void BindDDLType()
        {
            IList<VideoType> types = VideoTypeManager.GetAllVideoTypes();
            cbVideoType.Items.Clear();
            foreach (VideoType type in types)
            {
                ListItem li = new ListItem();
                li.Value = type.ID.ToString();
                li.Text = type.Title;
                cbVideoType.Items.Add(li);
            }
        }

        protected void btnUp_Click(object sender, EventArgs e)
        {
            if (this.fuInsertLogo.PostedFile.FileName != "")
            {
                lblILogo.Text = FileUploads(this, fuInsertLogo, "..\\..\\UpLoad\\videoImage\\");
                lblIMessage.Text = "图片上传成功！";
                this.imgInsertLogo.ImageUrl = "../../UpLoad/videoImage/" + lblILogo.Text;
            }
            else
            {
                lblIMessage.Text = "请选择要上传的图片！";
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (this.FileUpload1.PostedFile.FileName != "")
            {
                Label1.Text = FileUploads(this, FileUpload1, "..\\..\\UpLoad\\videoImage\\");
                Label2.Text = "图片上传成功！";
                this.Image1.ImageUrl = "../../UpLoad/videoImage/" + Label1.Text;
            }
            else
            {
                Label2.Text = "请选择要上传的图片！";
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (txtTitle.Text.Trim() != "")
            {
                int id = Convert.ToInt32(Request.QueryString["id"]);
                Models.VideoInfo photo = VideoInfoManager.GetVideoInfoByID(id);
                string types = "";
                for (int i = 0; i < cbVideoType.Items.Count; i++)
                {
                    if (cbVideoType.Items[i].Selected == true)
                    {
                        if (types == "")
                        {
                            types = cbVideoType.Items[i].Value;
                        }
                        else
                        {
                            types += "," + cbVideoType.Items[i].Value;
                        }
                    }
                }
                photo.TypeID = types;
                photo.Title = txtTitle.Text.Trim();
                if (txtPoint.Text.Trim().Equals(""))
                {
                    photo.Point = 0;
                }
                else
                {
                    photo.Point = Convert.ToSingle(txtPoint.Text.Trim());
                }
                if (txtCount.Text.Trim().Equals(""))
                {
                    photo.Count = 0;
                }
                else
                {
                    photo.Count = Convert.ToInt32(txtCount.Text.Trim());
                }
                photo.Location = "";
                photo.VideoContent = FCKeditor1.Value;
                photo.Image = lblILogo.Text;
                photo.Author = txtAuthor.Text.Trim();
                photo.AuthorPhoto = Label1.Text;
                photo.UploadDate = DateTime.Now;
                photo.XUHAO = Convert.ToInt32(txtXUHAO.Text.Trim());
                VideoInfoManager.ModifyVideoInfo(photo);

                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "aaa", "alert('修改微力视频信息成功。');window.opener.location.replace('Video_List.aspx');window.close();", true);
            }
            else
            {
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "aaa", "alert('请输入视频的标题。');", true);
            }

        }

        #region FileUp
        public static string FileUploads(Page page, FileUpload fileProduct, string url)
        {
            if (!fileProduct.HasFile)
            {
                return "出错";

            }
            try
            {
                string urlimg = DateTime.Now.ToString("hhmmss") + fileProduct.FileName;
                fileProduct.SaveAs(page.Server.MapPath(url) + urlimg);
                return urlimg;
            }
            catch
            {
                return "出错";
            }
        }
        #endregion
    }
}
